package com.stores.action;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.stores.model.Datastore;
import com.stores.model.Variation;

public class ManageVariationAction {

	/**
	 * 
	 * @param category
	 */
	public void Create(Variation variation) {
		DatastoreService datastore = Datastore.get();
		Entity variationEntity = new Entity(Variation.class.getSimpleName());
		variationEntity.setProperty("name", variation.getName());
		variationEntity.setProperty("description", variation.getDescription());
		variationEntity.setProperty("categoryId", variation.getCategoryId());
		variationEntity.setProperty("parentId", variation.getParentId());

		datastore.put(variationEntity);
	}

	/**
	 * 
	 * @param category
	 * @return
	 */
	public boolean Update(Variation variation) {
		DatastoreService datastore = Datastore.get();
		Query variationQuery = new Query(Variation.class.getSimpleName());
		variationQuery.addFilter(
				Entity.KEY_RESERVED_PROPERTY,
				FilterOperator.EQUAL,
				KeyFactory.createKey(Variation.class.getSimpleName(),
						variation.getId()));
		PreparedQuery query = datastore.prepare(variationQuery);
		Entity variationEntity = query.asSingleEntity();
		if (variationEntity == null) {
			return false;
		} else {
			variationEntity.setProperty("name", variation.getName());
			variationEntity.setProperty("description",
					variation.getDescription());
			variationEntity
					.setProperty("categoryId", variation.getCategoryId());
			variationEntity.setProperty("parentId", variation.getParentId());
			datastore.put(variationEntity);
		}
		return true;
	}

	/**
	 * 
	 * @return
	 */
	public List<Variation> Retrive() {
		DatastoreService datastore = Datastore.get();
		Query variationQuery = new Query(Variation.class.getSimpleName());
		PreparedQuery query = datastore.prepare(variationQuery);
		Iterable<Entity> variationEntities = query.asIterable();

		List<Variation> variations = new ArrayList<Variation>();

		for (Entity c : variationEntities) {
			Variation variation = new Variation();
			variation.setId(c.getKey().getId());
			variation.setName(c.getProperty("name").toString());
			variation.setDescription(c.getProperty("description").toString());
			variation.setCategoryId(Long.parseLong(c.getProperty("categoryId")
					.toString()));
			variation.setParentId(Long.parseLong(c.getProperty("parentId")
					.toString()));
			variations.add(variation);
		}
		return variations;
	}

	/**
	 * 
	 * @param categoryId
	 * @return
	 */
	public List<Variation> Retrieve(long categoryId) {
		DatastoreService datastore = Datastore.get();
		Query variationQuery = new Query(Variation.class.getSimpleName());
		variationQuery.addFilter("categoryId", FilterOperator.EQUAL, categoryId);
		PreparedQuery query = datastore.prepare(variationQuery);
		Iterable<Entity> variationEntities = query.asIterable();

		List<Variation> variations = new ArrayList<Variation>();

		for (Entity c : variationEntities) {
			Variation variation = new Variation();
			variation.setId(c.getKey().getId());
			variation.setName(c.getProperty("name").toString());
			variation.setDescription(c.getProperty("description").toString());
			variation.setCategoryId(Long.parseLong(c.getProperty("categoryId")
					.toString()));
			variation.setParentId(Long.parseLong(c.getProperty("parentId")
					.toString()));
			variations.add(variation);
		}
		return variations;
	}
}
